import {
  getAdressList
} from "../../utils/config";
import store from "../../utils/factory";

Page({
  data: {
    list: [],
    mode: "",
    noData: false
  },
  onLoad: function (e) {
    console.log(e.type);
    if (e) {
      this.setData({
        mode: e.type
      });
      store.set("mode", e.type);
    }
  },
  onShow() {
    this.getAdressList();
  },
  getAdressList() {
    getAdressList().then(res => {
      if (res.rows.length == 0) {
        this.setData({
          noData: true
        });
      } else {
        this.setData({
          noData: false
        });
      }
      res.rows.map(item => {
        item.typeStr = item.type == 3 ? "自提" : "送货上门"
      });
      this.setData({
        list: res.rows
      });

    });
  },
  add() {
    wx.navigateTo({
      url: "../addressHandle/addressHandle?type=add"
    });
  },
  edit(e) {
    let obj = e.currentTarget.dataset.detail;
    if (this.data.mode == "choice") {
      store.set("choiceAddress", obj);
      // 跳转到页面 A
      wx.navigateBack({
        delta: 1
      });
      return false;
    }
    store.set("addressDetail", obj);
    wx.navigateTo({
      url: "../addressHandle/addressHandle?type=edit"
    });
  },
  editAddress(e) {
    let obj = e.currentTarget.dataset.detail;
    store.set("addressDetail", obj);
    wx.navigateTo({
      url: "../addressHandle/addressHandle?type=edit"
    });
  }
});